#
# @lc app=leetcode.cn id=216 lang=python
#
# [216] 组合总和 III
#

# @lc code=start
class Solution(object):
    def combinationSum3(self, k, n):
        """
        :type k: int
        :type n: int
        :rtype: List[List[int]]
        """
        self.k = k
        self.n = n
        self.result = []
        self.result_items = []
        self.loop(1)
        return self.result

    def loop(self, start):
        if len(self.result_items) == self.k:
            if sum(self.result_items) == self.n:
                self.result.append(self.result_items[:])
            return

        for i in range(start, 9-self.k+len(self.result_items)+2):
            self.result_items.append(i)
            self.loop(i+1)
            self.result_items.pop()
        
# @lc code=end

